/*
 *  Copyright 2009 Utkin Dmitry
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
 
/* 
 *  This file is part of the WSF Staff project.
 *  Please, visit http://code.google.com/p/staff for more information.
 */

// This file generated by staff_codegen
// Service Implementation

#include <rise/common/Log.h>
#include <rise/common/ExceptionTemplate.h>
#include <rise/xml/XMLNode.h>
#include <staff/common/Config.h>
#include <staff/component/SharedContext.h>
#include <staff/component/Service.h>
#include <staff/component/ComponentConfig.h>
#include <staff/component/ComponentConfigManager.h>
#include "ServiceLocator.h"
#include "SubService.h"
#include "CalcServiceContext.h"
#include "CalcServiceImpl.h"

namespace samples
{
  namespace calc
  {
    CCalcServiceImpl::CCalcServiceImpl():
      m_pConfig(NULL)
    {
    }

    CCalcServiceImpl::~CCalcServiceImpl()
    {
    }

    int CCalcServiceImpl::Add(int nA, int nB) const
    {
      return nA + nB;  // result
    }

    int CCalcServiceImpl::Sub(int nA, int nB) const
    {
      const rise::CString& sID = CCalcServiceContext::GetContext().GetServiceID(this);

      CSubService* pSubServiceImpl = 
          reinterpret_cast<CSubService*>(staff::CServiceLocator::Inst().LocateService("samples.calc.SubService", sID));
      
      RISE_ASSERTES(pSubServiceImpl != NULL, rise::CLogicNoItemException, 
          "Service [samples.calc.SubService] with id [" + sID + "] not found");

      return pSubServiceImpl->Sub(nA, nB);
    }

    void CCalcServiceImpl::SetMem(int nMem)
    {
      GetConfig().Config().GetOrAddSubNode("Mem").NodeContent() = nMem;
      GetConfig().SaveConfig();
    }

    int CCalcServiceImpl::GetMem() const
    {
      return GetConfig().Config()["Mem"].AsInt();  // result
    }

    staff::CComponentConfig& CCalcServiceImpl::GetConfig() const
    {
      if (m_pConfig == NULL)
      {
        m_pConfig = &staff::CComponentConfigManager::Inst().GetComponentConfig("calc", "CalcService.xml", true);
      }

      return *m_pConfig;
    }

  }
}
